package com.ruoyi.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.course.domain.TeacherRole;
import com.ruoyi.student.domain.StudentCourse;
import com.ruoyi.student.mapper.StudentCourseMapper;
import com.ruoyi.student.service.IStudentCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;

import java.util.List;
import java.util.Map;

import com.ruoyi.course.mapper.CourseMapper;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;

/**
 * 课程Service业务层处理
 *
 * @author 刘红
 * @date 2023-11-28
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private StudentCourseMapper studentCourseMapper;
    @Autowired
    private IStudentCourseService iStudentCourseService;

    /**
     * 查询课程列表
     *
     * @param course 课程
     * @return 课程
     */
    @Override
    public List<Course> selectCourseList(Course course) {
        // 获取当前用户角色
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        // 假如当前用户是老师 那么只能看到自己的信息
        boolean flag = false;
        boolean isStudent = false;
        for (SysRole role : roles) {
            if (role.getRoleId() == 100L) {
                flag = true;
                break;
            }
//            if(role.getRoleId() == 101L){
//                isStudent = true;
//                break;
//            }
        }
        if (flag) {
            course.setTeacherId(loginUser.getUser().getUserId());
        }
        List<Course> courseList = courseMapper.selectCourseList(course);
        return courseList;
    }

    @Override
    public List<TeacherRole> listAllTeacherUser() {
        return courseMapper.listAllTeacherUser();
    }

    @Override
    public int addStudentCourse(Long id) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        boolean isStudent = false;
        for (SysRole role : roles) {
            if (role.getRoleId() == 101L) {
                isStudent = true;
                break;
            }
        }
        if (isStudent) {
            // 只有是学生才能参与选课
            // 查询课程信息
            Course course = courseMapper.selectById(id);
            // 判断当前课程是否已经选满
            if (course.getNumber() >= 0L) {
                // 在去查看 已选的多少人
                QueryWrapper<StudentCourse> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("course_id", id);
                Long num = iStudentCourseService.getBaseMapper().selectCount(queryWrapper);
                //如果已经选满 那就 不允许继续选择
                if (num >= course.getNumber()) {
                    return 0;
                }
                // 如果已经选择这个课 那就不选
                QueryWrapper<StudentCourse> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("course_id", id);
                queryWrapper1.eq("user_id", loginUser.getUser().getUserId());
                Long num1 = iStudentCourseService.getBaseMapper().selectCount(queryWrapper1);
                if (num1 > 0) return 0;
                // 如果没有选满 那就可以继续选择
                StudentCourse studentCourse = new StudentCourse();
                BeanUtils.copyProperties(course, studentCourse);
                studentCourse.setCourseId(id);
                studentCourse.setUserId(loginUser.getUser().getUserId());
                studentCourse.setCourseName(course.getName());
                studentCourse.setId(null);
                studentCourseMapper.insert(studentCourse);
                return 1;
            }
            //return courseMapper.addStudentCourse(id,loginUser.getUser().getUserId());
        }
        return 0;
    }


    private LambdaQueryWrapper<Course> buildQueryWrapper(Course query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<Course> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(query.getName()), Course::getName, query.getName());
        lqw.eq(query.getType() != null, Course::getType, query.getType());
        lqw.eq(query.getClassroom() != null, Course::getClassroom, query.getClassroom());
        lqw.eq(query.getTeacherId() != null, Course::getTeacherId, query.getTeacherId());
        lqw.orderByDesc(Course::getCreateTime);
        return lqw;
    }

}
